package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderConverter orderConverter;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();
        try {
            //参数校验
            request.requestCheck();
            //分页
            PageHelper.startPage(request.getPage(), request.getSize());
            //查询订单按照条件
            new Order();
            Example example = new Example(Order.class);
            example.createCriteria().andEqualTo("userId", request.getUserId());
            //排序
            if (!StringUtils.isEmpty(request.getSort())) {
                example.orderBy(request.getSort()).desc();
            } else {
                example.orderBy("createTime").desc();
            }
            List<Order> orders = orderMapper.selectByExample(example);
            List<OrderDetailInfo> list = new ArrayList<>();
            //遍历
            for (Order order : orders) {
                //获得orderId
                String orderId = order.getOrderId();
                //求出对应orderId的OrderItems
                OrderItem orderItemForQuery = new OrderItem();
                orderItemForQuery.setOrderId(orderId);
                List<OrderItem> orderItems = orderItemMapper.select(orderItemForQuery);
                //求出对应orderId的orderShippings
                // OrderShipping orderShippingForQuery = new OrderShipping();
                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
                //装换对象
                OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);
                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                //组装对象
                orderDetailInfo.setOrderItemDto(orderItemDtos);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);
                list.add(orderDetailInfo);
            }

            PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
            response.setDetailInfoList(list);
            response.setTotal(orderPageInfo.getTotal());
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("OrderQueryServiceImpl.orderList Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {
        OrderDetailResponse response = new OrderDetailResponse();
        try {
            // 参数校验
            request.requestCheck();
            // 查询订单数据
            Order order = orderMapper.selectByPrimaryKey(request.getOrderId());
            // 判空
            if (order == null) {
                throw new BizException(OrderRetCode.DB_EXCEPTION.getCode(), "该订单不存在");
            }
            response = orderConverter.order2res(order);
            // 查询商品数据
            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(request.getOrderId());
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            response.setOrderItemDto(orderItemDtos);
            // 查询物流
            Example example = new Example(OrderShipping.class);
            example.createCriteria().andEqualTo("orderId", request.getOrderId());
            OrderShipping orderShipping = orderShippingMapper.selectOneByExample(example);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            response.setOrderShippingDto(orderShippingDto);

            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("OrderQueryServiceImpl.orderDetail Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
